# -*- coding: utf-8 -*-
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome()  # 实例化浏览器对象
driver.maximize_window()
wait = WebDriverWait(driver, 5)  # 实例化等待对象
login_url = "http://120.25.127.201:18001/user/login.html"
driver.get(login_url)  # 在新的窗口打开url
username="15574113907"
password="123456"
wait.until(ec.presence_of_element_located((By.XPATH, "//input[@id='txtUName']"))).send_keys(username)
wait.until(ec.presence_of_element_located((By.XPATH, "//input[@id='txtPassword']"))).send_keys(password)
wait.until(ec.presence_of_element_located((By.XPATH, "//input[@id='btnLogin']"))).click()
# 根据username出现在首页作为等待条件，确保首页正常出现
wait.until(ec.text_to_be_present_in_element((By.LINK_TEXT,username),username))


locator = ("link text", "云上夕轮")
wait.until((ec.visibility_of_element_located(locator))).click()

# 显性等待发表评论按钮，并进行点击
locator = ("link text", "发表评论")
wait.until((ec.visibility_of_element_located(locator))).click()

# 显性等待评论输入框，并进行输入
content = f"发表评论-{str(int(time.time()))}"
locator = ("id", "txtComment")
wait.until((ec.visibility_of_element_located(locator))).send_keys(content)

# 显性等待发表按钮，并进行点击
locator = ("css selector", ".fr>.btn_ora")
wait.until((ec.visibility_of_element_located(locator))).click()

# 显性等待评论提示数据
locator = ("css selector", ".layui-layer-content")
ele_text = wait.until((ec.visibility_of_element_located(locator))).text
assert "评价成功" in ele_text, "评论失败，提示框中的内容与预期不符"
